import java.util.Scanner;
//0-O 1-X 2-空白
public class homework_5 {
    public static void main(String[] args){
        int[][] array = new int[3][3];
        int count = 0;
        int row;
        int column;
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                array[i][j] = 2;
            }
        }
        printtictactoc3(array);
        System.out.println("○先手，X后手");
        while(count<9){
            if(count<9) {
                do {
                    System.out.println("请○下棋");
                    do {
                        System.out.print("○，下在哪一行？");
                        row = input.nextInt();
                        if (row < 1 || row > 3) {
                            System.out.println("你只能输入1/2/3。");
                        }
                    } while (row < 1 || row > 3);
                    do {
                        System.out.print("○，下在哪一列？");
                        column = input.nextInt();
                        if (column < 1 || column > 3) {
                            System.out.println("你只能输入1/2/3。");
                        }
                    } while (column < 1 || column > 3);
                    if (array[row - 1][column - 1] == 0 || array[row - 1][column - 1] == 1) {
                        System.out.println("你不能在已经下过棋的位置下棋。");
                    }
                } while (array[row - 1][column - 1] == 0 || array[row - 1][column - 1] == 1);
                array[row - 1][column - 1] = 0;
                count++;
            }
            printtictactoc3(array);
            if (checktictactoe3(array)!=2){
                if(checktictactoe3(array)==0){
                    System.out.println("○赢了");
                    return;
                }
                if(checktictactoe3(array)==1){
                    System.out.println("X赢了");
                    return;
                }
            }
            if(count<9) {
                do {
                    System.out.println("请X下棋");
                    do {
                        System.out.print("X，下在哪一行？");
                        row = input.nextInt();
                        if (row < 1 || row > 3) {
                            System.out.println("你只能输入1/2/3。");
                        }
                    } while (row < 1 || row > 3);
                    do {
                        System.out.print("X，下在哪一列？");
                        column = input.nextInt();
                        if (column < 1 || column > 3) {
                            System.out.println("你只能输入1/2/3。");
                        }
                    } while (column < 1 || column > 3);
                    if (array[row - 1][column - 1] == 0 || array[row - 1][column - 1] == 1) {
                        System.out.println("你不能在已经下过棋的位置下棋。");
                    }
                } while (array[row - 1][column - 1] == 0 || array[row - 1][column - 1] == 1);
                array[row - 1][column - 1] = 1;
                printtictactoc3(array);
                count++;
            }
            if (checktictactoe3(array)!=2){
                if(checktictactoe3(array)==0){
                    System.out.println("○赢了");
                    return;
                }
                if(checktictactoe3(array)==1){
                    System.out.println("X赢了");
                    return;
                }
            }
        }
        System.out.println("棋局结束。平局");
    }
    public static int checktictactoe3(int[][] a){
            boolean check;
            int num = 0;
            CHECK:
            while (true) {
                R:
                for (int i = 0; i < a.length; i++) {
                    for (int j = 0; j < a[i].length; j++) {
                        if ((a[i][j] == a[i][0]) && j != 0 && a[i][0]!=2) {
                            num++;
                        }
                        if (num >= 2) {
                            check = true;
                            return a[i][0];
                        }
                    }
                    num = 0;
                }
                num = 0;
                C:
                for (int i = 0; i < a.length; i++) {
                    for (int j = 0; j < a[i].length; j++) {
                        if ((a[j][i] == a[0][i]) && j != 0 && a[0][i]!=2) {
                            num++;
                        }
                        if (num >= 2) {
                            check = true;
                            return a[0][i];
                        }
                    }
                    num = 0;
                }
                num = 0;
                T:
                for (int i = 0; i < a.length; i++) {
                    if ((a[i][i] == a[0][0]) && i != 0 && a[0][0]!=2)  {
                        num++;
                    }
                    if (num >= 2) {
                        check = true;
                        return a[0][0];
                    }
                }
                num = 0;
                AT:
                for (int i = 0; i < a.length; i++) {
                    if ((a[i][a.length - i - 1] == a[0][a.length - 1]) && i != 0 && a[0][a.length-1]!=2) {
                        num++;
                    }
                    if (num >= 2) {
                        check = true;
                        return a[0][a.length-1];
                    }
                }
                check = false;
                return 2;
            }
    }
    public static void printtictactoc3(int[][] a){
        for (int i = 0; i < a.length; i++) {
            System.out.println("———————");
            for (int j = 0; j < a[i].length; j++) {
                System.out.print("|");
                switch (a[i][j]){
                    case 0:{
                        System.out.print("○");
                        break;
                    }
                    case 1:{
                        System.out.print("X");
                        break;
                    }
                    case 2:{
                        System.out.print(" ");
                        break;
                    }
                }
            }
            System.out.println("|");
        }
        System.out.println("———————");
    }
}